package com.example.test1.controller;

import com.example.test1.controller.request.ModifyDrinkRequest;
import com.example.test1.model.Drink;
import com.example.test1.service.IDrinkService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Controller
@RequestMapping("/drink")
public class DrinkController {
    @Autowired
    private IDrinkService drinkService;

    @PostMapping(value = "/add",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Drink addNewDrink(@RequestBody @Valid NewDrinkRequest newDrink) {
        log.info("创建新饮品{}", newDrink);
        Drink drink = new Drink();
        BeanUtils.copyProperties(newDrink,drink);
        drink = drinkService.addNewDrink(drink);
        log.info("创建成功");
        return drink;
    }

    @GetMapping("/")
    public List<Drink> listDrinks(){
        log.info("查询饮品菜单");
        return drinkService.getAllDrinks();
    }

    @GetMapping("/{id}")
    public Drink getDrinkById(@PathVariable Long id){
        log.info("查询饮品, id:{}",id);
        Optional<Drink> drinkOptional = drinkService.getDrinkById(id);
        if (drinkOptional.isPresent()){
            return drinkOptional.get();
        }else{
            return null;
        }
    }

}
